#import "AppDelegate.h"
#import <Flutter/Flutter.h>
#import "GeneratedPluginRegistrant.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    FlutterViewController *controller = (FlutterViewController*)self.window.rootViewController;
    
//    __weak typeof(self) weakSelf = self;
    FlutterMethodChannel* appChannel = [FlutterMethodChannel methodChannelWithName:@"plugins.io/app"
                                                                   binaryMessenger:controller.binaryMessenger];
    [appChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
        NSArray *methodArray = [NSArray arrayWithObjects:@"getSplashImageResource",nil];
        NSUInteger index = [methodArray indexOfObject:call.method];
        switch (index) {
            // getSplashImageResource
            case 0:{
                UIImage* splashImage = [UIImage imageNamed:@"start_wel_page"];
                if (splashImage == nil) {
                    return result(nil);
                }
                result([FlutterStandardTypedData typedDataWithBytes:UIImageJPEGRepresentation(splashImage, 1.0f)]);
            }
            break;
                
            default:{
                result(FlutterMethodNotImplemented);
            }
            break;
        }
//        if ([@"getImageResource" isEqualToString:call.method]) {
//            NSArray *lauchImages = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
//            //            if (weakSelf.splashImageView != nil) {
//            //                [weakSelf.splashImageView removeFromSuperview];
//            //                self.splashImageView = nil;
//            //            }
//            result(nil);
//        } else {
//            result(FlutterMethodNotImplemented);
//        }
    }];
    
    // 初始化一个简单的和启动页一样的图覆盖在上面，等到flutter启动完毕在关闭，防止出现闪一下
    //    self.splashImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"start_wel_page"]];
    //    self.splashImageView.frame = self.window.frame;
    //    self.splashImageView.contentMode = UIViewContentModeScaleToFill;
    
    // 加入视图，然后在回掉去关闭
    //    [controller.view addSubview:self.splashImageView];
    
    // Override point for customization after application launch.
    [GeneratedPluginRegistrant registerWithRegistry:self];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end
